<template>
  <div>
    <el-button
      type="primary"
      plain
      icon="el-icon-plus"
      size="mini"
      @click="handleAdd"
      v-hasPermi="['system:swxx:add']"
    >卫健新增
    </el-button>

    <!-- 卫健数据新增对话框 -->
    <el-dialog title="卫健数据新增" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <div class="module-section">
          <div class="module-header">
            <i class="el-icon-first-aid-kit"></i>
            <span class="module-title">卫健数据</span>
            <span class="module-remark">（所有带*字段均为必填）</span>
          </div>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="报告地区名称" prop="bgdqmc">
                <el-input v-model="form.bgdqmc" placeholder="请输入报告地区名称" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="报告卡编号" prop="bgbh">
                <el-input v-model="form.bgbh" placeholder="请输入报告卡编号" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="填报单位代码" prop="tbdwdm">
                <el-input v-model="form.tbdwdm" placeholder="请输入填报单位代码" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="填报单位名称" prop="tbdwmc">
                <el-input v-model="form.tbdwmc" placeholder="请输入填报单位名称" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="死者姓名" prop="szxm">
                <el-input v-model="form.szxm" placeholder="请输入死者姓名" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别" prop="xb">
                <el-select v-model="form.xb" placeholder="请选择性别" clearable style="width: 100%">
                  <el-option label="男" value="0" />
                  <el-option label="女" value="1" />
                  <el-option label="未知" value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="证件类型" prop="zjlx">
                <el-select v-model="form.zjlx" placeholder="请选择证件类型" clearable style="width: 100%">
                  <el-option label="居民身份证" value="居民身份证" />
                  <el-option label="护照" value="护照" />
                  <el-option label="军官证" value="军官证" />
                  <el-option label="其他" value="其他" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="证件号码" prop="zjhm">
                <el-input v-model="form.zjhm" placeholder="请输入证件号码" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="出生日期" prop="csrq">
                <el-date-picker
                  v-model="form.csrq"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择出生日期"
                  style="width: 100%"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="民族" prop="mz">
                <el-input v-model="form.mz" placeholder="请输入民族" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="生前详细地址" prop="szxxdz">
                <el-input v-model="form.szxxdz" placeholder="请输入生前详细地址" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="户籍详细地址" prop="hjxxdz">
                <el-input v-model="form.hjxxdz" placeholder="请输入户籍详细地址" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="死亡日期" prop="swrq">
                <el-date-picker
                  v-model="form.swrq"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择死亡日期"
                  style="width: 100%"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="死亡地点" prop="swdd">
                <el-input v-model="form.swdd" placeholder="请输入死亡地点" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="家属姓名" prop="jtxm">
                <el-input v-model="form.jtxm" placeholder="请输入可联系的家属姓名" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="家属联系电话" prop="jtdh">
                <el-input v-model="form.jtdh" placeholder="请输入家属联系电话" clearable />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addSwxx } from '@/api/system/swxx'

export default {
  name: 'Add',
  data() {
    return {
      open: false,
      form: {
        bgdqmc: null,
        bgbh: null,
        tbdwdm: null,
        tbdwmc: null,
        szxm: null,
        xb: null,
        zjlx: null,
        zjhm: null,
        csrq: null,
        mz: null,
        szxxdz: null,
        hjxxdz: null,
        swrq: null,
        swdd: null,
        jtxm: null,
        jtdh: null
      },
      rules: {
        bgdqmc: [{ required: true, message: '报告地区名称不能为空', trigger: 'blur' }],
        bgbh: [{ required: true, message: '报告卡编号不能为空', trigger: 'blur' }],
        tbdwdm: [{ required: true, message: '填报单位代码不能为空', trigger: 'blur' }],
        tbdwmc: [{ required: true, message: '填报单位名称不能为空', trigger: 'blur' }],
        szxm: [{ required: true, message: '死者姓名不能为空', trigger: 'blur' }],
        xb: [{ required: true, message: '性别不能为空', trigger: 'change' }],
        zjlx: [{ required: true, message: '证件类型不能为空', trigger: 'change' }],
        zjhm: [{ required: true, message: '证件号码不能为空', trigger: 'blur' }],
        csrq: [{ required: true, message: '出生日期不能为空', trigger: 'change' }],
        mz: [{ required: true, message: '民族不能为空', trigger: 'blur' }],
        szxxdz: [{ required: true, message: '生前详细地址不能为空', trigger: 'blur' }],
        hjxxdz: [{ required: true, message: '户籍详细地址不能为空', trigger: 'blur' }],
        swrq: [{ required: true, message: '死亡日期不能为空', trigger: 'change' }],
        swdd: [{ required: true, message: '死亡地点不能为空', trigger: 'blur' }],
        jtxm: [{ required: true, message: '家属姓名不能为空', trigger: 'blur' }],
        jtdh: [{ required: true, message: '家属联系电话不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleAdd() {
      this.reset()
      this.open = true
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        bgdqmc: null,
        bgbh: null,
        tbdwdm: null,
        tbdwmc: null,
        szxm: null,
        xb: null,
        zjlx: null,
        zjhm: null,
        csrq: null,
        mz: null,
        szxxdz: null,
        hjxxdz: null,
        swrq: null,
        swdd: null,
        jtxm: null,
        jtdh: null
      }
      this.resetForm('form')
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          addSwxx(this.form).then(response => {
            this.$modal.msgSuccess('卫健数据新增成功')
            this.open = false
            this.$emit('success')
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.module-section {
  margin-bottom: 24px;
  padding: 16px;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  background: #f8f9fa;
}

.module-header {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 2px solid #67C23A;
}

.module-header i {
  font-size: 18px;
  margin-right: 8px;
  color: #67C23A;
}

.module-title {
  font-size: 16px;
  font-weight: 600;
  color: #67C23A;
  margin-right: 8px;
}

.module-remark {
  font-size: 12px;
  color: #f56c6c;
}
</style>
